package huoye.shops.huoye.operation.activity;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.previewimage.PrevieImage;
import com.codeideology.android.previewimage.PrevieImageActivity;
import com.codeideology.android.utils.BarUtils;
import com.codeideology.android.utils.DateTimeFormat;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.pickerview.TimePickerView;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseTakePhotoActivity;
import huoye.shops.base.UserManager;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.operation.model.AddImage;
import huoye.shops.huoye.operation.model.GuestsDetailData;
import huoye.shops.huoye.operation.model.VipGustsData;
import huoye.shops.huoye.operation.presenter.GuestsSetPresenter;
import huoye.shops.huoye.operation.view.IGuestsSetView;
import huoye.shops.utils.PickerViewUtils;
import huoye.shops.widget.RoundImageView;

public class GuestsSetActivity extends BaseTakePhotoActivity implements IGuestsSetView{


    @BindView(R.id.guests_set_rv)
    RecyclerView recyclerView;


    @BindView(R.id.guests_set_rv_head)
    RoundImageView rvHead;
    @BindView(R.id.guests_set_tv_name)
    TextView tvName;
    @BindView(R.id.guests_set_tv_phone)
    TextView tvPhone;
    @BindView(R.id.guests_set_tv_power_content)
    TextView tvPrlContent;
    @BindView(R.id.guests_set_et_power_content)
    EditText etPrlContent;
    @BindView(R.id.guests_set_tv_expire_selet_time)
    TextView tvExpireTime;

    @BindView(R.id.include_add_cut_tv)
    TextView tvAddCutNumber;


    //因为只有一张图片所以特意拉出来测试
    @BindView(R.id.guests_set_iv_add)
    ImageView ivAddPic;

    @BindView(R.id.guests_set_iv_delete)
    ImageView ivDelPic;

    @OnClick(R.id.guests_set_iv_add)
    void onAddPicClick(){
        if(isEdit){
            //makeToast("我是编辑");
        }else {
          //  makeToast("我是新增");
            takePhoto();
        }
    }

    @OnClick(R.id.guests_set_iv_delete)
    void onDeletePicClick(){
        if(isEdit){
            //makeToast("我是编辑");
        }else {
          //  makeToast("我是新增");
            mGuestsSetPresenter.deleteOssImg(imgPath);
        }
    }




    TimePickerView pvTime;
    private GuestsSetPresenter mGuestsSetPresenter;
    private VipGustsData vipGustsData;

    @OnClick(R.id.guests_set_ll_expire_selet_time)
    void  onSeletExpireTimeClick(){
        if(isEdit){

        }else {
            if (pvTime == null) {
                pvTime = PickerViewUtils.showEventYearMonthDay(this, new Date(), new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                       // makeToast(DateTimeFormat.formatDate(date));
                        tvExpireTime.setText(DateTimeFormat.formatDate(date));
                    }
                });
            }
            pvTime.show();
        }


    }

    @OnClick(R.id.guests_set_btn_affirm)
    void  onAffirmClick(){
        if(isEdit){
          //  makeToast("我是编辑");
            mGuestsSetPresenter.modifyPrivilegedGuest(vipGustsData.getShopSpeGuestId(),mNumber+"");
        }else {
            //makeToast("我是新增");
            String etContent = etPrlContent.getText().toString();
            String userId = UserManager.getInstance().getUserId();
            String endTime = tvExpireTime.getText().toString();
            mGuestsSetPresenter.addPrivilegedGuest(userId,vipGustsData.getUserId(),etContent,imgPath,mNumber+"",endTime);
        }

    }


    int mNumber = 1;

    @OnClick(R.id.include_add)
    void  onAddClick(){
        mNumber += 1;
        tvAddCutNumber.setText(mNumber+"");
    }

    @OnClick(R.id.include_cut)
    void  onCutClick(){
        if(mNumber <= 1 ){

        }else {
            mNumber -= 1;
        }
        tvAddCutNumber.setText(mNumber+"");
    }



    @Override
    protected int getContentViewId() {
        return R.layout.activity_guests_set;

    }

    //用来判断编辑 是不能点击的 新增才可以进行点击
    private boolean isEdit;

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("嘉宾设置");
        vipGustsData = getIntentData();
        this.isEdit = vipGustsData.isEdit();
        mGuestsSetPresenter = new GuestsSetPresenter();
        mGuestsSetPresenter.addTaskListener(this);
        mGuestsSetPresenter.onStart();


        editViewShow(isEdit);
        //  showAdapter();
    }

    private void editViewShow(boolean isEdit){
        if(isEdit){
           // makeToast("我是编辑");
            tvPrlContent.setVisibility(View.VISIBLE);
            etPrlContent.setVisibility(View.GONE);
            ivDelPic.setVisibility(View.GONE);
            //编辑才请求接口
            mGuestsSetPresenter.getPrivilegedGuestDetail(vipGustsData.getShopSpeGuestId());
        }else {
           // makeToast("我是新增");
            tvPrlContent.setVisibility(View.GONE);
            etPrlContent.setVisibility(View.VISIBLE);
            ivDelPic.setVisibility(View.GONE);

            tvName.setText(vipGustsData.getNickName());
            Glide.with(this)
                    .load(vipGustsData.getHeadImg())
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(rvHead);
            tvPhone.setText(vipGustsData.getMobile());
        }
    }



    private String imgPath = "";
    @Override
    public void takeSuccess(TResult result) {
        if (result != null) {

            TImage image = result.getImage();
            imgPath = image.getCompressPath();

            File file = new File(imgPath);
            Glide.with(this)
                    .load(Uri.fromFile(file))
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(ivAddPic);



            String userShopId = UserManager.getInstance().getUserShopId();
            mGuestsSetPresenter.uploadOssImg(userShopId,"shop",imgPath);

        }
    }

    @Override
    public void getDetail(GuestsDetailData guestsDetailData) {
        if(vipGustsData != null){
            tvName.setText(vipGustsData.getNickName());
            Glide.with(this)
                        .load(vipGustsData.getHeadImg())
                        .dontAnimate()
                        .placeholder(R.mipmap.ic_wait_logo)
                        .error(R.mipmap.ic_error)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(rvHead);
            tvPhone.setText(vipGustsData.getMobile());
            tvPrlContent.setText(vipGustsData.getPrlContent());
        }

        if(StringUtils.isNotEmpty(guestsDetailData.getImgUrls())){
            Glide.with(this)
                    .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+guestsDetailData.getImgUrls())
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(ivAddPic);
        }

        tvExpireTime.setText(guestsDetailData.getEndTime());
        mNumber = guestsDetailData.getPrlNum();
        tvAddCutNumber.setText(mNumber+"");
    }

    @Override
    public void setUpload(OssImageData ossImageData) {
        imgPath = ossImageData.getFilePath();
        ivDelPic.setVisibility(View.VISIBLE);

    }

    @Override
    public void setDelete() {
        ivAddPic.setImageResource(R.mipmap.ic_add_image);
        ivDelPic.setVisibility(View.GONE);
    }
}
